<template>
  <div class="item-config">
    <el-form :model="cloudRadialConfigForm"
              label-position="top"
              label-width="100px"
             ref="cloudradial-form"
             :rules="cloudRadialConfigRules">
      <el-form-item :label="$t('custom.connector.cloudradial.url')"
                    prop="url">
        <el-input v-model="cloudRadialConfigForm.url"
                  :placeholder="$t('custom.connector.cloudradial.interurl')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('custom.connector.cloudradial.key')"
                    prop="key">
        <el-input v-model="cloudRadialConfigForm.key"
                  :placeholder="$t('custom.connector.cloudradial.interKey')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('custom.connector.cloudradial.secret')"
                    prop="secret">
        <el-input v-model="cloudRadialConfigForm.secret"
                  :type="SecretKeyCheck?'text':'password'"
                  :placeholder="$t('custom.connector.cloudradial.interSecret')">
          <i slot="suffix"
             :class="SecretKeyCheck?'cuifont icon_eye_show':'cuifont icon_eye_hide'"
             @click="SecretKeyCheck=!SecretKeyCheck"></i>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   :loading="testLoading"
                   style="width:165px;margin-top: 6px;"
                   size="small"
                   @click="startTestConfig">{{ $t('custom.connector.savequickbookspath') }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { Component } from './modules/Component.js'
import Trail from '@/components/storybook/mixins/trail'

export default {
  mixins: [Trail],

  props: {
    cloudRadialConfigForm: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      SecretKeyCheck: false,
      passwordChecked: false,
      cloudRadialConfigRules: {
        url: [
          {
            // required: true,
            message: this.$t('custom.connector.cloudradial.interurl'),
            trigger: 'blur',
            pattern: '[^ \x22]+'
          }
        ],
        key: [
          {
            required: true,
            message: this.$t('custom.connector.cloudradial.interKey'),
            trigger: 'blur',
            pattern: '[^ \x22]+'
          }
        ],
        secret: [
          {
            required: true,
            message: this.$t('custom.connector.cloudradial.interSecret'),
            trigger: 'blur',
            pattern: '[^ \x22]+'
          }
        ]
      },
      testLoading: false
    }
  },
  created () {
  },
  methods: {
    cancelThisConfig () {
      this.cloudRadialConfigForm = {
        url: '',
        key: '',
        secret: ''
      }
    },
    startTestConfig () {
      this.validateConfigForm().then(() => {
        this.testLoading = true
        const params = {
          ...this.cloudRadialConfigForm,
          tenantId: this.$store.state.user.tenantCode,
          subjectCode: 'CLOUDRADIAL'
        }
        this.$interface.taskManager
          .postClientData(params)
          .then(({ data: res }) => {
            if (res.code !== 0) {
              this.testLoading = false
              Component.$emit('getConnectorConfig')
              return this.$message.error(res.msg)
            }
            this.testLoading = false
            this.synctdData()
            return this.$message.success(res.msg)
          }).catch(() => {
            this.testLoading = false
          })
      })
    },
    validateConfigForm () {
      return this.$refs['cloudradial-form'].validate()
    },
    synctdData (itemInfo) {
      const params = {
        tenantId: this.$store.state.user.tenantCode,
        subjectCode: 'CLOUDRADIAL'
      }
      this.$interface.taskManager.syncClientData(params).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }

        if (itemInfo && res.data && res.data.sync) {
          itemInfo.sync.lastTime = res.data.sync.lastTime
          itemInfo.sync.status = res.data.sync.status
        }
        Component.$emit('getConnectorConfig')
        return this.$message.success(res.msg)
      }).catch(() => {
      })
    }
  }
}
</script>

<style scoped></style>
